#include "main.h"
#include "ParseTS_Head.h"
#include "ParsePAT.h"
#include "ParsePMT.h"
#include "ParseNIT.h"
#include "ParseEIT.h"

unsigned char TSdata[204] = {0};
TS_PAT *TS_PackagePatPoint;
TS_PMT *TS_PackagePmtPoint;
TS_NIT *TS_PackageNitPoint;
TS_PMT *PmtPoint;
TS_NIT *NitPoint;


TS_Package_Head ParseTs_Head(FILE *pTsFile, int TsPosition, int TsPackage_len, unsigned char PackageHead_data [4])
{
    int value = 0;
    int PMTCount = 0;
    int nit_value = 0;
    int bat_value = 0; 
    int sdt_value = 0;
    int eit_value = 0;
    TS_Package_Head TS_PackageHead = {0};

    rewind(pTsFile);    //重置文件内部指针位置

    List();             //创建各个表的首链表

    while(fgetc(pTsFile) != EOF)
    {   
        fseek(pTsFile, TsPosition - 1, SEEK_CUR);                   //判断有效位置 
        if(fgetc(pTsFile) == 0x47)
        {
            fseek(pTsFile, TsPosition - 1, SEEK_CUR);

            if(fseek(pTsFile, TsPosition + 1, SEEK_CUR) == -1)
            {
                printf("fseek error!\n");
            }
            
            if(fseek(pTsFile, TsPosition - 1, SEEK_CUR) == -1)
            {   
                printf("fseek error!");
            }
            
            fseek(pTsFile, TsPackage_len, SEEK_CUR);
            
            if(fgetc(pTsFile) == 0x47)
            {   
                fseek(pTsFile, -TsPackage_len, SEEK_CUR);
                fseek(pTsFile, TsPosition - 1, SEEK_CUR);

                fread(TSdata, 1, TsPackage_len, pTsFile);

                TS_PackageHead = ParseTS_Type();                   //解析表头获取表PID等信息
            }

            TsPackage_len = 188;

            if(TS_PackageHead.PID == 0x00 && value == 0)           //过滤出PAT表进行解析；value防止重复循环
            {
                TS_PackagePatPoint = NULL;
                TS_PackagePatPoint = ParseTS_PAT(TsPackage_len, TS_PackageHead);
                
                value++;
            }

            if(TS_PackagePatPoint != NULL)                          //确定获取到PAT表
            {      
                if((TS_PackageHead.PID == TS_PackagePatPoint->astPAT_Program[PMTCount].program_map_PID) &&  //过滤出PMT表
                            (TS_PackagePatPoint->astPAT_Program[PMTCount].program_map_PID != 0))
                {
                    ParseTS_PMT(TsPackage_len, TS_PackageHead);
        
                    PMTCount++;
                }
            }

            if(TS_PackageHead.PID == 0x0010)                        //过滤出NIT表并分离有效负载的首表
            {  
                if(TS_PackageHead.payload_unit_start_indicator == 1 && TSdata[5] == 0x40)
                {
                    nit_value = 1;
                    ParseTS_NIT_1(TsPackage_len, TS_PackageHead);
                }

                if(TS_PackageHead.payload_unit_start_indicator == 0 && nit_value != 0)
                {
                    ParseTS_NIT_0(TsPackage_len, TS_PackageHead);
                }
            }

            if(TS_PackageHead.PID == 0X0011)                    //过滤出BAT表并分离有效负载的首表
            {
                if(TS_PackageHead.payload_unit_start_indicator == 1 && TSdata[5] == 0x4a)
                {
                    bat_value = 1; 
                   ParseTS_BAT_1(TsPackage_len, TS_PackageHead);
                }

                if(TS_PackageHead.payload_unit_start_indicator == 0 && bat_value != 0)
                {
                    ParseTS_BAT_0(TsPackage_len, TS_PackageHead);
                }
            }

            if(TS_PackageHead.PID == 0X0011)                    //过滤出SDT表并分离有效负载的首表
            {
                if(TS_PackageHead.payload_unit_start_indicator == 1 && TSdata[5] == 0x42)
                {
                    sdt_value = 1; 
                   ParseTS_SDT_1(TsPackage_len, TS_PackageHead);
                }

                if(TS_PackageHead.payload_unit_start_indicator == 0 && sdt_value != 0)
                {
                    ParseTS_SDT_0(TsPackage_len, TS_PackageHead);
                }
            }

            if(TS_PackageHead.PID == 0x0012)                    //过滤出EIT表并分离有效负载的首表
            {
                if(TS_PackageHead.payload_unit_start_indicator == 1 && (TSdata[5] == 0x4E 
                            || (0x50 < TSdata[5] && TSdata[5] < 0x5F)))
                {
                    eit_value = 1;
                    ParseTS_EIT_1(TsPackage_len, TS_PackageHead);
                }

                if(TS_PackageHead.payload_unit_start_indicator == 1 && eit_value != 0)
                {
                    ParseTS_EIT_0(TsPackage_len, TS_PackageHead);
                }
            }
        }
    }
}
